package controllers;

import play.mvc.*;
import models.Article;
import models.Group;

import java.util.*;
import models.Category;
import models.User;
import play.data.validation.Valid;
import play.i18n.Messages;
import play.libs.Crypto;

public class Application extends Controller {

    public static void index() {
        List<Article> entities = models.Article.find("order by created desc").fetch(3);
        render(entities);
    }

    public static void about() {

        render();

    }

    public static void contact() {
        render();

    }

    public static void securite() {
        render();


    }

    public static void articleIndex() {
        List<Article> entities = models.Article.find("order by created desc").fetch();
        render(entities);
    }

    public static void articleShow(Long id) {
        Article entity = Article.findById(id);
        render(entity);
    }
        public static void showCat()
    {
        List<Article> entities = models.Article.find("select article from Article article where category_id=8").fetch();
        render(entities);
        
        
        
    }

    public static void inscription() {
        render();
    }

    public static void inscriptionSave(@Valid User entity) {
        entity.password = Crypto.encryptAES(entity.password);
        if (validation.hasErrors()) {
            flash.error(Messages.get("validation"));
            entity.password = null;
            render("@inscription", entity);

        }
        User verif = User.find("byEmail", entity.email).first();
        if (verif != null) {
            flash.error(Messages.get("Users.exist"));
            entity.password = null;
            Group group = Group.findById((long) 4);
            entity.groups = group;
            render("@inscription", entity);

        } else {
            entity.save();
            flash.success(Messages.get("Users.created"));
            Application.index();
        }

    }
}